package com.xy.deepclone;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/10 15:55
 * @description
 */

/**
 * 序列化就是将对象写到流中的过程，写到流中的对象是原有对象的一个拷贝，而原对象仍然存在于内存中。
 * 通过序列化实现的拷贝不仅可以复制对象本身，而且可以复制其引用的成员对象，因此通过序列化将对象写到一个流中，
 * 再从流中将其读出来，即可实现深拷贝。需要注意的是序列化对象的类必须实现Serializable接口，否则无法实现序列化操作。
 *
 * Java语言提供的Cloneable接口和Serializable接口的代码非常简单，
 * 它们都是空接口，这种空接口也称为标识接口，标识接口中没有任何方法的定义，
 * 其作用是告诉JRE这些接口的实现类是否具有某个功能，实现或不实现就是该功能的体现，
 * 如是否支持克隆、是否支持序列化等。
 */
public class DeepCloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        /*DeepPrototype deepPrototype = new DeepPrototype();
        deepPrototype.name ="jack";
        deepPrototype.deepCloneableTarget = new DeepCloneableTarget("dog","dog ...");
        DeepPrototype clone = deepPrototype.clone();
        System.out.println("deepPrototype.deepCloneableTarget.hashCode() = " + deepPrototype.deepCloneableTarget.hashCode());
        System.out.println("clone.deepCloneableTarget.hashCode() = " + clone.deepCloneableTarget.hashCode());
        System.out.println(deepPrototype.deepCloneableTarget == clone.deepCloneableTarget);*/

        DeepPrototype deepPrototype = new DeepPrototype();
        deepPrototype.name ="jack";
        deepPrototype.deepCloneableTarget = new DeepCloneableTarget("dog","dog ...");
        DeepPrototype clone = deepPrototype.deepClone();
        System.out.println(deepPrototype);
        System.out.println(clone);
        System.out.println("deepPrototype.deepCloneableTarget.hashCode() = " + deepPrototype.deepCloneableTarget.hashCode());
        System.out.println("clone.deepCloneableTarget.hashCode() = " + clone.deepCloneableTarget.hashCode());
        System.out.println(deepPrototype.deepCloneableTarget == clone.deepCloneableTarget);
    }
}
